﻿using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Generic;



namespace BackendsCheckTool
{
    public class Backend {
        public string name;
        public string ipAddress;
        public int port;

        public Backend( string name, string ipAddress, int port ) {
            this.name = name;
            this.ipAddress = ipAddress;
            this.port = port;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Backend> backends = GetBackendsFromFile(args);
            int backendIndex = 0;
            Console.WriteLine( "" );
            Console.WriteLine( "{0,52}","Backends Check Tool 1.0" );
            Console.WriteLine( "" );
            Console.WriteLine( "Host: {0}",System.Environment.MachineName );
            Console.WriteLine( "Time: {0:MMM dd, yyyy hh:mm:ss}", DateTime.Now);
            Console.WriteLine( "" );
            Console.WriteLine( "{0,-4}{1,-25}{2,-19}{3,-6}", "#","Backend","IP:Port","Status" );
            Console.WriteLine( "{0,-4}{1,-25}{2,-19}{3,-6}", "--- ", "------------------------ ", "------------------ ", "------ " );
            foreach ( Backend backend in backends ) {
                TcpClient tcpClient = new TcpClient();
                backendIndex += 1;
                try {
                    Console.Write( "{0:000 }{1,-25}{2,-19}", backendIndex, backend.name,backend.ipAddress+":"+backend.port );
                    tcpClient.Connect( backend.ipAddress, backend.port );
                    if ( tcpClient.Connected ) {
                        Console.WriteLine( "{0,-6}","OK" );
                    }
                } catch ( Exception) {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine( "{0,-6}", "FAILED" );
                    Console.ResetColor();
                } finally {
                    tcpClient.Close();
                    tcpClient = null;
                }
            }

            Console.WriteLine( "" );
        }

        public static List<Backend> GetBackendsFromFile(string[] args){
            try {
                if ( args.Length < 1 ) {
                    Console.Write( "Wrong Input" );
                }
                string inputFilePath = args[0];

                List<Backend> result = new List<Backend>();

                using ( FileStream fileStream = new FileStream( inputFilePath, FileMode.Open, FileAccess.Read ) ) {
                    using ( StreamReader streamReader = new StreamReader( fileStream ) ) {
                        while ( true ) {
                            string line = streamReader.ReadLine();
                            if ( line == null ) {
                                return result;
                            }

                            string[] lineContent= line.Split( ',' );
                            result.Add( new Backend( lineContent[0].Trim(), lineContent[1].Trim(), int.Parse( lineContent[2].Trim() ) ) );
                        }
                    }
                }
            } catch ( Exception ) {
                Console.WriteLine( "Error While Parsing Input File." );
                return null;
            }
        }
    }
}
